/**
 * Created by wenchuan on 2018/5/3.
 */
define(function () {

    var self,
        app = win.app,
        orders = [],
        address = {
            GWCM010: '', //客户手机号
            GWCM011: '',//客户编号
            GWCM020: '',//详细地址
            GWAKL004: '',//客户名称
            GWCM021: '',//
            GWCM022: '',//
            GWCM023: '',//
            GWCM024: '',//
            GWCM025: '',//
            GWCM026: '',//

        },
        others = {
            countAmount: 0
        },
        pageUi,
        orderSubmitArgs
    ;

    function setCountAmount() {
        var c = 0;
        orders.forEach(function (v) {
            c += (Number(v.GWCM004) * Number(v.GWCM003));
        });
        others.countAmount = c;
    }

    var OrderSubmit = app.Module.extend({

        init: function () {
            self = this;
            self._data = {};
            orderSubmitArgs = app.pageParameters['ordersubmit_page'];
            orders.pushArray(orderSubmitArgs.items);
            self._super({
                _tag: 'p_ordersubmit'
            }, win);
        },
        subData: function () {
            if (!address.GWCM011 || address.GWCM011 == '') {
                app.toast('请选择客户！');
                return;
            }
            address.orders = orders;
            app.ajax({
                xurl: "api/wx/gwat100/submitData",
                loading: true,
                errmsg: '提交操作异常',
                xdata: {
                    obj: app.jsonString(address)
                },
                cb: function (result) {
                    if (result.ok) {
                        app.alert({
                            msg: '意向订单提交成功！',
                            ok: function () {
                                app.back(1);
                            }
                        });
                        orderSubmitArgs.cb();
                    }
                }
            });
        },
        _lazy: function () {
            this._super();
            setCountAmount();
            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005
            });
        },
        _ready: function () {
            Vue.component('ordersubmit-template', {
                template: app.getByid(doc, 'cartitem-template'),
                props: {
                    model: Array
                },
                methods: {}
            });

            new Vue({
                el: app.getByid(doc, 'address-box'),
                data: {
                    address: address
                },
                methods: {
                    showCustomers: function () {
                        app.openPage({
                            // src: "yxsjxd/customers.html",
                            // argsKey: 'customers_page',
                            src: "hn-bfjltb/customers.html",
                            args: {
                                cb: function (obj) {
                                    address.GWCM011 = obj.GWAK001;
                                    address.GWAKL004 = obj.GWAKL004;

                                    address.GWCM006 =obj.GWAK017;
                                    address.GWCM007 =obj.GWAK018;
                                    address.GWCM008 =obj.GWAK019;
                                    address.GWCM009 =obj.GWAK031;
                                    address.GWCM020 = obj.GWAK028;
                                    address.GWCM010 = obj.GWAK030;
                                    address.GWCM021 = obj.GWAK020;
                                    address.GWCM022 = obj.GWAK021;
                                    address.GWCM023 = obj.GWAK022;
                                    address.GWCM024 = obj.GWAK023;
                                    address.GWCM025 = obj.GWAK024;
                                    address.GWCM026 = obj.GWAK025;
                                    address.GWCM027 = obj.GWAK026;
                                    address.GWCM028 = obj.GWAK027;

                                }
                            }
                        });
                    }
                }
            });

            new Vue({
                el: app.getByid(doc, 'contentCenter'),
                data: {
                    items: orders
                },
                methods: {
                    serchData: function () {
                        self._refresh();
                    }
                }
            });

            new Vue({
                el: app.getByid(doc, 'contentFooter'),
                data: others,
                methods: {
                    subOrder: function () {
                        app.confirm({
                            msg: '确定提交意向订单？',
                            ok: function () {
                                self.subData();
                            }
                        });

                    }
                }
            });

        }
    });

    return new OrderSubmit();

});